package cn.tedu.application.service.impl;

import cn.tedu.application.mapper.ApplicationMapper;
import cn.tedu.application.pojo.dto.ApplicationQuery;
import cn.tedu.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.application.pojo.entity.Application;
import cn.tedu.application.service.ApplicatinService;
import cn.tedu.attendance.pojo.vo.PageResult;
import cn.tedu.base.enums.ApplicationStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicatinService {
    @Autowired
    private ApplicationMapper applicationMapper;

//    @Autowired
//    AuditService auditService;
//    @Autowired
//    UserMapper userMapper;
    @Override
    public List<Application> selectAll() {
        return applicationMapper.selectAll();
    }


    @Override
    public PageResult<Application> pageQuery(ApplicationQuery applicationQuery) {
        Integer count = applicationMapper.queryCount(applicationQuery);
        if(count == null || count < 1){
            return new PageResult<>();
        }

        List<Application> applications = applicationMapper.queryData(applicationQuery);
        return new PageResult<>(count,applications);
    }



    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存用户申请业务,applicationSaveParam={}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        application.setCreateTime(new Date());
        //当新增时,请假申请表的状态为已发送
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        applicationMapper.add(application);

        /*TODO*/
        //还需要调用审批业务层,为当前申请单同步新增对应的审批数据
        //auditService.insertAudit(application);
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请单业务:id={}",id);
        Application application = new Application();
        application.setId(id);
        //撤销需要将申请单状态改为"撤销"
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        /*Todo*/
        //还需要删除此申请单对应的所有审批单数据
        //auditMapper.deleteByApplicationId(id);

    }

}
